<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace switcher\utils;

final class ReflectionHelperTest extends \PHPUnit_Framework_TestCase
{
	public function testShouldGetReflectionProperty()
	{
		$object = new \Exception('message', 123);
		$reflectionClass = new \ReflectionClass($object);
		$reflectionProperty = ReflectionHelper::getReflectionProperty($reflectionClass, 'code');
		$this->assertEquals($reflectionProperty->getName(), 'code');
	}
	
	public function testShouldGetReflectionPropertyFromSuperClass()
	{
		$object = new \InvalidArgumentException('message', 123);
		$reflectionClass = new \ReflectionClass($object);
		$reflectionProperty = ReflectionHelper::getReflectionProperty($reflectionClass, 'code');
		$this->assertEquals($reflectionProperty->getName(), 'code');
		$this->assertEquals($reflectionProperty->getDeclaringClass()->getName(), 'Exception');
	}
	
	public function testShouldNotGetUnexistingReflectionProperty()
	{
		$object = new \Exception('message', 123);
		$reflectionClass = new \ReflectionClass($object);
		$this->setExpectedException('ReflectionException');
		$reflectionProperty = ReflectionHelper::getReflectionProperty($reflectionClass, 'unexisting');
	}
	
	public function testShouldSetUnaccessileProperty()
	{
		$object = new \Exception('message', 123);
		ReflectionHelper::setProperty($object, 'code', 321);
		$this->assertEquals($object->getCode(), 321);
	}
	
	public function testShouldSetUnexistingProperty()
	{
		$object = new \Exception('message', 123);
		ReflectionHelper::setProperty($object, 'unexisting', 'value');
		$this->assertEquals($object->unexisting, 'value');
	}
	
	public function testShouldCreateObjectWithProperties()
	{
		$array = array(
			'@type' => 'Exception',
			'message' => 'message',
			'code' => 123,
			'ignored' => null
		);
		
		$object = ReflectionHelper::createObject($array);
		
		$this->assertTrue( is_object($object) );
		$this->assertEquals(get_class($object), 'Exception');
		$this->assertEquals($object->getMessage(), 'message');
		$this->assertEquals($object->getCode(), 123);
	}
	
	public function testShouldNotCreateObjectWithUnexistingType()
	{
		$array = array(
			'@type' => 'UnkownException',
			'message' => 'message',
			'code' => 123,
			'ignored' => null
		);
		
		$this->setExpectedException('ReflectionException');
		ReflectionHelper::createObject($array);
	}
	
	public function testShouldNotCreateObjectWithoutType()
	{
		$array = array(
			'type' => 'Exception',
			'message' => 'message',
			'code' => 123,
			'ignored' => null
		);
		
		$this->setExpectedException('InvalidArgumentException');
		ReflectionHelper::createObject($array);
	}
	
	public function testShouldOverrideObject()
	{
		$object = new \Exception('message', 123);
		
		$array = array(
			'message' => 'new message',
			'code' => 321,
			'ignored' => null
		);
		
		ReflectionHelper::overrideObject($object, $array);
		
		$this->assertEquals($object->getMessage(), 'new message');
		$this->assertEquals($object->getCode(), 321);
	}
	
	public function testShouldCreateProperties()
	{
		$xml = '
		<root>
			<message>message</message>
			<code>123</code>
		</root>';
		
		$element = simplexml_load_string($xml);
		
		$expected = array(
			'message' => 'message',
			'code' => 123
		);
		
		$actual = ReflectionHelper::createProperties($element);
		
		$this->assertEquals($expected, $actual);
	}
	
	public function testShouldCreateRecursiveProperties()
	{
		$xml = '
		<root>
			<message>message</message>
			<code>123</code>
			<previous>
				<message>previous message</message>
				<code>321</code>
			</previous>
		</root>';
		
		$element = simplexml_load_string($xml);
		
		$expected = array(
			'message' => 'message',
			'code' => 123,
			'previous' => array(
				'message' => 'previous message',
				'code' => 321
			)
		);
		
		$actual = ReflectionHelper::createProperties($element);
		
		$this->assertEquals($expected, $actual);
	}
}

?>